<?php

	class users_DAO extends CI_Model{

		public function __construct(){
			parent::__construct();
			$this->load->database();
		}

		public function getAll(){
			$query = $this->db->get("users");
			return $query->result_array();
		}
		
		public function get($username){
			$query = $this->db->query("SELECT username, email, level FROM users WHERE username='{$username}'");
			if($query->num_rows() == 1){
				$tempQuery = $query->result();
				return $tempQuery[0];
			} else {
				return null;
			}
		}
		
		public function add($users){
			$this->db->insert("users", $users);
			return ($this->db->affected_rows() != 1) ? false : true;
		}
		
		public function update($username, $email, $level){
			$data = array(
               'email' => $email,
			   'level' => $level
            );
			$this->db->where('username', $username);
			$this->db->update('users', $data);
			return ($this->db->affected_rows() != 1) ? false : true;
		}
		
		public function changePassword($username, $password){
			$data = array(
               'password' => $password
            );
			$this->db->where('username', $username);
			$this->db->update('users', $data);
			return ($this->db->affected_rows() != 1) ? false : true;
		}
		
		public function delete($username){
			$this->db->where('username', $username);
			$this->db->delete('users');
			return ($this->db->affected_rows() != 1) ? false : true;
		}
		
		public function login($username, $password){
			$this->db->where('username', $username);
			$this->db->where('password', $password);
			$query=$this->db->get("users");
			return $query->result_array();
		}
	}
?>